﻿using Microsoft.EntityFrameworkCore;
using RollCall.Data;
using RollCall.Pages;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Navigation;

namespace RollCall.ViewModel
{
    public class SettingPageViewModel : BaseViewModel
    {
        private ObservableCollection<Group> _groups;
        /// <summary>
        /// 所有分组
        /// </summary>
        public ObservableCollection<Group> Groups
        {
            get => _groups;
            set
            {
                _groups = value;
                OnPropertyChanged();
            }
        }

        private Group _currentGroup;

        /// <summary>
        /// 当前分组
        /// </summary>
        public Group CurrentGroup
        {
            get => _currentGroup;
            set
            {
                _currentGroup = value;
                OnPropertyChanged();
                RefreshStudent();
            }
        }

        private string _newStudent;
        /// <summary>
        /// 新增学员
        /// </summary>
        public string NewStudent
        {
            get => _newStudent;
            set
            {
                _newStudent = value;
                OnPropertyChanged();
            }
        }

        private ObservableCollection<Student> _students;
        /// <summary>
        /// 所有学生
        /// </summary>
        public ObservableCollection<Student> Students
        {
            get => _students;
            set
            {
                _students = value;
                OnPropertyChanged();
            }
        }


        private string _newGroup;
        /// <summary>
        /// 新增分组
        /// </summary>
        public string NewGroup
        {
            get => _newGroup;
            set
            {
                _newGroup = value;
                OnPropertyChanged();
            }
        }
        private readonly NavigationService _navigationService;
        public string Type { get; set; }
        public ICommand ReturnCommand { get; set; }

        /// <summary>
        /// 新增学员命令
        /// </summary>
        public ICommand AddStudentCommand { get; set; }

        /// <summary>
        /// 新增分组命令
        /// </summary>
        public ICommand AddGroupCommand { get; set; }


        /// <summary>
        /// 当前分组移除学生
        /// </summary>
        public ICommand RemoveStudent { get; set; }

        /// <summary>
        /// 删除当前分组
        /// </summary>
        public ICommand RemoveGroupCommand { get; set; }

        private readonly Subject _subject;

        public SettingPageViewModel(NavigationService navigationService, string type)
        {
            _navigationService = navigationService;
            Type = type;
            _subject = (Subject)Enum.Parse(typeof(Subject), Type);

            RefreshGroup();
            RefreshStudent();

            ReturnCommand = new RelayCommand(s =>
              {
                  _navigationService.Navigate(new RollPage(Type));
              }, s => true);

            //新增学生
            AddStudentCommand = new RelayCommand(s =>
              {
                  if (string.IsNullOrWhiteSpace(NewStudent))
                  {
                      MessageBox.Show("学生名称不能为空");
                      return;
                  }
                  if (CurrentGroup == null)
                  {
                      MessageBox.Show("请先创建分组");
                  }
                  else
                  {
                      var student = new Student
                      {
                          Name = NewStudent,
                          GroupId = CurrentGroup.Id,
                      };
                      App.MyDbContext.Add(student);
                      App.MyDbContext.SaveChanges();
                      RefreshStudent();
                      NewStudent = "";
                  }
              }, s => true);

            //添加分组
            AddGroupCommand = new RelayCommand(s =>
            {
                if (string.IsNullOrWhiteSpace(NewGroup))
                {
                    MessageBox.Show("分组名称不能为空");
                    return;
                }
                if (App.MyDbContext.Groups.Any(a => a.Name == NewGroup && a.Subject == _subject))
                {
                    MessageBox.Show("分组名称重复");
                    return;
                }

                var group = new Group()
                {
                    Name = NewGroup,
                    Subject = _subject,
                };
                App.MyDbContext.Add(group);
                App.MyDbContext.SaveChanges();
                Groups.Add(group);
                CurrentGroup = group;
                NewGroup = "";
            }, s => true);

            //删除学生名单
            RemoveStudent = new RelayCommand(s =>
              {
                  var student = App.MyDbContext.Students.Find(s);
                  if (student != null)
                  {
                      App.MyDbContext.Remove(student);
                      App.MyDbContext.SaveChanges();
                  }
                  RefreshStudent();
              });

            //删除整个分组里面的所有信息
            RemoveGroupCommand = new RelayCommand(s =>
              {
                  if (CurrentGroup == null) return;
                  var groupId = CurrentGroup.Id;
                  var group = App.MyDbContext.Groups.Find(groupId);
                  App.MyDbContext.Groups.Remove(group);

                  var students = App.MyDbContext.Students.Where(w => w.GroupId == group.Id).ToList();
                  App.MyDbContext.RemoveRange(students);
                  App.MyDbContext.SaveChanges();
                  RefreshGroup();
                  RefreshStudent();
              });
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        private void RefreshStudent()
        {
            if (CurrentGroup == null)
            {
                Students = new ObservableCollection<Student>();
                return;
            }
            var students = App.MyDbContext
                .Students
                .AsNoTracking()
                .Where(w => w.GroupId == CurrentGroup.Id)
                .OrderBy(o => o.Name)
                .ToList();
            Students = new ObservableCollection<Student>(students);
        }
        /// <summary>
        /// 刷新分组
        /// </summary>
        private void RefreshGroup()
        {
            var groups = App.MyDbContext.Groups.Where(w => _subject == w.Subject).ToList();
            Groups = new ObservableCollection<Group>(groups);
            CurrentGroup = Groups.FirstOrDefault();
        }
    }
}
